/* 
* Class name: StartGame *
* 
* Version info: v4.0 *
* 
* Description: This class is for starting the game
* 
* Copyright by Group4 from CmpE450 course *
*/ 

package com.GameSpace.SameGame;

import java.util.Random;

import com.GameSpace.CoreAssets.BoardSize;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.GameSpace.CoreAssets.Color;
import com.GameSpace.CoreAssets.Game;
import com.GameSpace.CoreAssets.GameManager;
import com.GameSpace.CoreAssets.HeaderFooterWrapper;
import com.GameSpace.SameGame.R;
import com.GameSpace.CoreAssets.ToolBarType;
import com.GameSpace.CoreAssets.Toolbar;
import com.GameSpace.CoreAssets.ToolbarHomeButton;
import com.GameSpace.CoreAssets.ToolbarLabelItem;
import com.GameSpace.SameGame.GameBoard;

public class StartGame extends Game {
	
	private static final String logtag = "StartGame";
	boolean hasLevel = true;
	//a GameBoard and a Ball array is created for the game
	private GameBoard board;
	private Ball arr[]= new Ball[72];
	
	//an overridden method to initialize the Start game screen
	@Override
	public void onCreate( Bundle savedInstanceState ){
		
		super.onCreate(savedInstanceState);
		board = new GameBoard(this,this);
		Start();
	  	Log.d(logtag, "onCreateNewGame");
	    
	  	//defines variables to keep the toolbars
	  	ToolbarLabelItem scoreBoard;
	  	ToolbarLabelItem chainScore;
	  	ToolBarUndoButton undo_btn;
	  	View wrapper;
	  
	  	//initializes the toolbars
	    Toolbar f = new Toolbar(getBaseContext(),ToolBarType.FOOTER); 
	    Toolbar h = new Toolbar(getBaseContext(),ToolBarType.HEADER); 
    	h.setToolbarMargin(80);
    	h.setToolbarHeight(100);
    	
    	//create a new toolbar item to show the score of the user
    	scoreBoard = new ToolbarLabelItem(this);
    	scoreBoard.SetBackgroundColor(R.color.black);
    	scoreBoard.setFontColor(R.color.white);
    	scoreBoard.setFontSize(16);
    	h.addItem(scoreBoard);
    	
    	//create a new toolbar item to show the chain score of the selected chain
    	chainScore = new ToolbarLabelItem(this);
    	chainScore.SetBackgroundColor(R.color.black);
    	chainScore.setFontColor(R.color.white);
    	chainScore.setFontSize(16);
    	h.addItem(chainScore);
    	
    	//set views of the score and chain score to be used by game board
    	board.setScoreBoard(scoreBoard);
    	board.setChainBoard(chainScore);
    	
    	//initialize and set the undo button
	    h.updateLayout();
	    undo_btn = new ToolBarUndoButton(this);
	    board.setUndoButton(undo_btn);
	    f.addItem(undo_btn);
	    f.addHomeButton(new ToolbarHomeButton(this,StartGame.this));
	    f.updateLayout();
	    
	    wrapper=new HeaderFooterWrapper(this,board,f,h);
	    
	    setContentView(wrapper);
	    wrapper.requestFocus();
	}
	
	//an initializer function to create random array of balls according to difficulty
	public void Start(){
		
		//Background and Foreground colors are set
		Color.setBackgroundColor(getResources().getColor(R.color.white));
		Color.setForegroundColor(getResources().getColor(R.color.yellow));
		
		// The game board sizes are set to 9x8
		BoardSize.setSizeX(8);
		BoardSize.setSizeY(9);
		
		switch(GameManager.getLevel()){
		/*
		 * if the level is EASY Difficulty is set to 3 since there will be 4 different colored ball 
		 * types in EASY level.
		 */
		case GameManager.EASY:
			composeBallArray(0);
			(board).setDifficulty(3);
			break;
		/*
		 * if the level is MEDIUM Difficulty is set to 4 since there will be 4 different colored ball 
		 *types in MEDIUM level.
		 */
		case GameManager.MEDIUM:
			composeBallArray(1);
			(board).setDifficulty(4);
			break;
		/*
		 * if the level is HARD Difficulty is set to 5 since there will be 5 different colored ball 
		 * types in HARD level.
		 */
		case GameManager.HARD:
			composeBallArray(2);
			(board).setDifficulty(5);
			break;
		}
		
		board.setArray(arr);
	
	}
	
	//creates fills the random array of balls and set their colors
	public void composeBallArray( int difficulty ){
		int rndUpperBound = 0;
		int i;
		int j;
		Random rnd = new Random();
		
		for(i=0;i<72;i++){
	    	arr[i]= new Ball();
	    }
		if(difficulty == 0)
	    	rndUpperBound = 3;
	    else if(difficulty == 1)
	    	rndUpperBound = 4;
	    else
	    	rndUpperBound = 5;
    
		
		//9x8=72 balls' color ids are defined randomly.
	    for ( i = 0; i < BoardSize.getSizeX(); i++) {
	    	for ( j = 0; j <BoardSize.getSizeY(); j++) {
	    		
	    		//The randomness is provided here.the upper bound for the color id is determined by the difficulty level
	    		int temp= rnd.nextInt(rndUpperBound);
	    		if(temp==0){
	    			arr[i*9+j].setColor(0);
	    		}
	    		else if(temp==1){
	    			arr[i*9+j].setColor(1);
	    		}
	    		else if(temp==2){
	    			arr[i*9+j].setColor(2);
	    		}
	    		else if(temp==3){
	    			arr[i*9+j].setColor(3);
	    		}
	    		else if(temp==4){
	    			arr[i*9+j].setColor(4);
	    		}
	    	}
	    }
	}
	
	public void End(){
		
	}
}
